package com.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.entity.Complain;
import com.community.service.ComplainService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * (Complain)表控制层
 *
 * @author makejava
 * @since 2021-10-17 16:37:37
 */
@Controller
@RequestMapping("complain")
public class ComplainController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ComplainService complainService;

    @RequestMapping("complainlist")
    public String tolist() {
        return "complainlist";
    }

    @RequestMapping("complainmodify")
    public String complainmodify() {
        return "complainmodify";
    }

    /**
     * 分页查询所有数据
     *
     * @param page     分页对象
     * @param complain 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    @ResponseBody
    public R selectAll(Page<Complain> page, Complain complain) {
        QueryWrapper<Complain> wrapper = new QueryWrapper<>();
        wrapper.like(complain.getMyContent() != null, "my_content", complain.getMyContent());
        wrapper.between(complain.getBeginDate() != null && complain.getOverDate() != null,
                "create_date", complain.getBeginDate(), complain.getOverDate());
        wrapper.eq(complain.getStatus() != null, "status", complain.getStatus());
        wrapper.orderByDesc("create_date");
        return success(this.complainService.page(page, wrapper));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ResponseBody
    public R selectOne(@PathVariable Serializable id) {
        return success(this.complainService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param complain 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    @ResponseBody
    public R insert(@RequestBody Complain complain) {
        complain.setCreateDate(new Date());
        complain.setStatus(1);
        return success(this.complainService.save(complain));
    }

    /**
     * 修改数据
     *
     * @param complain 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    @ResponseBody
    public R update(Complain complain) {
        return success(this.complainService.updateById(complain));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    @ResponseBody
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.complainService.removeByIds(idList));
    }
}

